﻿using System;
using System.Linq;
using System.Web.Mvc;
using mvcForum.Core.Abstractions.Interfaces;
using mvcForum.Web.Areas.ForumAdmin.ViewModels.Update;
using mvcForum.Web.Controllers;
using mvcForum.Web.Interfaces;
using System.IO;
using mvcForum.Core.Interfaces.Data;

namespace mvcForum.Web.Areas.ForumAdmin.Controllers {

	public class SettingsController : ForumAdminBaseController {
		private readonly IConfiguration config;

		public SettingsController(IWebUserProvider userProvider, IContext context, IConfiguration config)
			: base(userProvider, context) {

			this.config = config;
		}

		[HttpGet]
		public ViewResult Index() {
			ForumSettingsViewModel model = new ForumSettingsViewModel(this.config, Server.MapPath("~/themes"));
			return View(model);
		}

		[HttpPost]
		public ViewResult Index(ForumSettingsViewModel model) {
			if (ModelState.IsValid) {
				this.config.MessagesPerPage = model.MessagesPerPage;
				this.config.TopicsPerPage = model.TopicsPerPage;
				this.config.AllowUserDefinedTheme = model.AllowUserDefinedTheme;
				this.config.AsynchronousAntiSpam = model.AsynchronousAntiSpam;
				this.config.DefaultCulture = model.DefaultCulture;
				this.config.DefaultTimezone = model.DefaultTimezone;
				this.config.MaxAttachmentsSize = model.MaxAttachmentsSize * 1000000;
				this.config.MaxFileSize = model.MaxFileSize * 1000000;
				this.config.PostsOnReply = model.PostsOnReply;
				//this.config.RobotEmailAddress = model.RobotEmailAddress;
				//this.config.RobotName = model.RobotName;
				this.config.ShowOldPostsOnReply = model.ShowOldPostsOnReply;
				this.config.SiteURL = model.SiteURL;
				this.config.AllowedExtensions = model.AllowedExtensions.Split(new Char[] { ';' }, StringSplitOptions.RemoveEmptyEntries).ToList();
				this.config.Theme = model.Theme;
				this.config.AllowQuickReply = model.AllowQuickReply;
				this.config.CheckForNews = model.CheckForNews;
				this.config.Editor = model.Editor;
				this.config.NewUserGroups = model.NewUserGroups.Select(i => Int32.Parse(i)).ToArray();
				this.config.ShowDeletedMessages = model.ShowDeletedMessages;
				this.config.ShowOnlineUsers = model.ShowOnlineUsers;
				this.config.InformOnQuarantine = model.InformOnQuarantine;
				this.config.RedirectUrlLoggedIn = model.RedirectUrlLoggedIn;
				this.config.RedirectUrlLoggedOut = model.RedirectUrlLoggedOut;
				this.config.RedirectUrlRegistrered = model.RedirectUrlRegistrered;
				this.config.AuthorNameSetting = model.AuthorName;
				this.config.Save();

				TempData["Saved"] = true;
			}
			String themeRoot = Server.MapPath("~/themes");
			// Issue #49
			if (Directory.Exists(themeRoot)) {
				model.Themes = Directory.GetDirectories(themeRoot).Select(d => d.Substring(themeRoot.Length + 1)).ToArray();
			}
			else {
				model.Themes = new String[] { };
			}
			return View(model);
		}

		[HttpGet]
		public ViewResult UserRegistration() {
			UserRegistrationViewModel model = new UserRegistrationViewModel(this.config);
			return View(model);
		}

		[HttpPost]
		// TODO: Remove this and get it working with AllowHtml instead!!
		[ValidateInput(false)]
		public ViewResult UserRegistration(UserRegistrationViewModel model) {
			if (ModelState.IsValid) {
				this.config.UseForumAccountController = model.UseForumAccountController;
				this.config.AllowLocalUsers = model.UseForumAccountController && model.AllowLocalUsers;
				this.config.AllowSignUp = model.UseForumAccountController && model.AllowLocalUsers && model.AllowSignUp;
				this.config.AllowOpenAuthUsers = model.UseForumAccountController && model.AllowOpenAuthUsers;
				this.config.RequireEmailValidation = model.UseForumAccountController && model.AllowLocalUsers && model.AllowSignUp && model.RequireEmailValidation;
				this.config.RequireRulesAccept = model.UseForumAccountController && model.AllowLocalUsers && model.AllowSignUp && model.RequireRulesAccept;

				this.config.ValidationSubject = model.ValidationSubject;
				this.config.ValidationBody = model.ValidationBody;

				this.config.ForgottenPasswordBody = model.ForgottenPasswordBody;
				this.config.ForgottenPasswordSubject = model.ForgottenPasswordSubject;

				this.config.SignUpRules = model.SignUpRules;

				this.config.Save();

				TempData["Saved"] = true;
			}
			return View(model);
		}

		[HttpGet]
		public ViewResult Mail() {
			MailViewModel model = new MailViewModel(this.config);
			return View(model);
		}

		[HttpPost]
		public ViewResult Mail(MailViewModel model) {
			if (ModelState.IsValid) {
				this.config.RobotEmailAddress = model.SenderEmailAddress;
				this.config.RobotName = model.SenderName;
				this.config.Save();
				TempData["Saved"] = true;
			}

			return View(model);
		}
	}
}